const jwt = require('jwt-simple');
const secretKey = "juseSecretKey";

module.exports = function (req, res, next) {
    if (!req.header('Authorization')) {
        return res.status(401).send({ message: '无请求头' });
    }
    const token = req.header('Authorization').split(' ')[1];
    let payload;
    try {
        payload = jwt.decode(token, secretKey);
    }
    catch (err) {
        return res.status(401).send({ message: err.message });
    }
    req.user = {};
    if (!payload.userid) {
        return res.status(401).send({ message: '无权限' });
    } else if (!payload.phone) {
        return res.status(402).send({ message: '没绑定手机号' });
    } else {
        req.user = payload;
        next();
    }
}